<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>SSE DEMO</h1>
  <button onclick="sendRequest('http:\/\/127.0.0.1:3000/req1')">发送请求1</button>
  <button onclick="sendRequest('http:\/\/127.0.0.1:3000/req2')">发送请求2</button>
  <a href="http://127.0.0.1:3000">去a页面</a>
  <br/>
  <div id="message"></div>
  <script>
    const msgEle = document.querySelector('#message')
    let id = 0
    function sendRequest(url) {
      var xhr = new XMLHttpRequest()
      xhr.open('GET', `${url}?id=${id}`)
      xhr.setRequestHeader('withCredentials', true)
      // id++
      xhr.onreadystatechange = function() {
        if(xhr.readyState === 4) {
          msgEle.innerHTML += xhr.responseText + "\n"
        }
      }
      xhr.send()
    }
  </script>
</body>
</html>